package com.mvvm.store.ui.delivery;

import android.content.SharedPreferences;
import android.view.KeyEvent;
import android.view.View;

import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.leo.utilspro.utils.ToastUtils;
import com.mvvm.store.R;
import com.mvvm.store.base.BaseActivity;
import com.mvvm.store.base.NormalViewModel;
import com.mvvm.store.bean.basebean.DeliveryInfoBean;
import com.mvvm.store.bean.basebean.DeliveryScanInfoBean;
import com.mvvm.store.bean.basebean.DeliverySubmitParams;
import com.mvvm.store.bean.basebean.ResponModel;
import com.mvvm.store.bean.basebean.ScanDeliveryBean;
import com.mvvm.store.databinding.FragmentDeliveryManagerBinding;
import com.mvvm.store.databinding.FragmentIntoManagerBinding;
import com.mvvm.store.roombatabase.DeliveryDatabase;
import com.mvvm.store.roombatabase.DeliveryInfoDao;
import com.mvvm.store.roombatabase.DeliveryScanDao;
import com.mvvm.store.roombatabase.DeliveryScanDatabase;
import com.mvvm.store.roombatabase.ScanDeliveryDao;
import com.mvvm.store.roombatabase.ScanDeliveryDatabase;
import com.mvvm.store.ui.delivery.adapter.DeliveryPagerAdapter;
import com.mvvm.store.ui.into.MaterialInfoFragment;
import com.mvvm.store.ui.into.MaterialSweptFragment;
import com.mvvm.store.ui.into.adaper.MaterialPagerAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by leo
 * on 2020/10/21.   出库管理
 */
public class DeliveryManagerActivity extends BaseActivity<DeliveryViewModel, FragmentDeliveryManagerBinding> {

    private List<Fragment> fragments=new ArrayList<>();

    private List<CharSequence> titleslist=new ArrayList<>();

    private String[] titles = new String[]{"出库信息", "已扫物料标签"};

    private DeliveryInfoDao deliveryInfoDao;
    private DeliveryScanDao deliveryScanDao;
    private String deliveryOrderId;
    public static boolean isDeliveryScan=false;
    private ScanDeliveryDao scanDeliveryDao;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_delivery_manager;
    }

    @Override
    protected void processLogic() {
        isDeliveryScan=false;
        deliveryInfoDao= DeliveryDatabase.getInstance(this).deliveryInfoDao();
        deliveryScanDao= DeliveryScanDatabase.getInstance(this).deliveryScanDao();
        scanDeliveryDao= ScanDeliveryDatabase.getInstance(this).scanDeliveryDao();
        SharedPreferences sharedPreferences = this.getSharedPreferences("deliveryInfo", MODE_PRIVATE);
        deliveryOrderId=sharedPreferences.getString("stockOrderNo","");
        System.out.println("stockOrderNo------------"+deliveryOrderId+"");
        binding.leoTitleBar.bar_left_btn.setBackgroundResource(R.mipmap.return_images);
        binding.leoTitleBar.bar_right_btn.setVisibility(View.VISIBLE);

        for (int i=0;i<titles.length;i++){
            titleslist.add(titles[i]);
        }
        DeliveryInfoFragment deliveryInfo=DeliveryInfoFragment.newInstance("deliveryInfo",binding.viewPager);
        DeliverySweptFragment deliverySwept=DeliverySweptFragment.newInstance("deliverySwept",binding.viewPager);
        fragments.add(deliveryInfo);
        fragments.add(deliverySwept);
        DeliveryPagerAdapter deliveryPagerAdapter=new DeliveryPagerAdapter(this.getSupportFragmentManager(),fragments,titleslist);
        binding.viewPager.setAdapter(deliveryPagerAdapter);
        binding.tablayout.setupWithViewPager(binding.viewPager);
        binding.tablayout.setTabMode(TabLayout.MODE_FIXED);
        binding.tablayout.getTabAt(0).select();
    }

    @Override
    protected void setListener() {
        binding.leoTitleBar.bar_left_btn.setOnClickListener(this::onClick);
        binding.leoTitleBar.bar_right_btn.setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bar_left_btn:
                this.finish();
                break;
            case R.id.bar_right_btn:
                deliverySubmit();
                break;
            default:
                break;
        }
    }
    //点击提交数据
    private void deliverySubmit(){
        //先判断数据是否合法提交
        if(isSubmit()){
            //提交
            SharedPreferences sharedPreferences =this.getSharedPreferences("deliveryUserInfo", MODE_PRIVATE);
            String userId=sharedPreferences.getString("deliveryUserId","");
            if("".equals(userId)){
                //提示他去扫码验证身份
                ToastUtils.showToast("提交前先请扫码验证身份");
                isDeliveryScan=true;
            }else {
                //组包进行提交
                DeliverySubmitParams deliverySubmitParams=new DeliverySubmitParams();
                deliverySubmitParams.setInputPro(userId);
                deliverySubmitParams.setOutStockOrderNo(deliveryOrderId);
                List<DeliveryInfoBean> deliveryInfoBeanLisToLoad= deliveryInfoDao.getDeliveryByOrderNumber(deliveryOrderId);
                List<DeliverySubmitParams.MaterialList> materialListList=new ArrayList<>();

                for(int i=0;i<deliveryInfoBeanLisToLoad.size();i++){

                    deliverySubmitParams.setAddress(deliveryInfoBeanLisToLoad.get(i).getAddress());
                    deliverySubmitParams.setId(deliveryInfoBeanLisToLoad.get(i).getId()+"");
                    deliverySubmitParams.setStatus(1);
                    deliverySubmitParams.setSupplierCode(deliveryInfoBeanLisToLoad.get(i).getSupplierCode());
                    deliverySubmitParams.setSupplierName(deliveryInfoBeanLisToLoad.get(i).getSupplierName());
                    DeliverySubmitParams.MaterialList materialList=new DeliverySubmitParams.MaterialList();
                    materialList.setBoxCount(deliveryInfoBeanLisToLoad.get(i).getRealToPack());
                    materialList.setId(deliveryInfoBeanLisToLoad.get(i).getId()+"");
                    materialList.setOutStockId(deliveryInfoBeanLisToLoad.get(i).getOutStockId());
                    materialList.setPartName(deliveryInfoBeanLisToLoad.get(i).getOddName());
                    materialList.setPartNumber(deliveryInfoBeanLisToLoad.get(i).getOddNumber());
                    materialList.setTotal(deliveryInfoBeanLisToLoad.get(i).getRealToNumber());
                    //添加标签码
                    List<String> tagCodeList=new ArrayList<>();
                    //查询扫码表的标签
                    List<DeliveryScanInfoBean>  deliveryScanInfoBeans=  deliveryScanDao.getDeliveryScanByOddNumber(deliveryInfoBeanLisToLoad.get(i).getOddNumber());
                    for (int j=0;j<deliveryScanInfoBeans.size();j++){
                        tagCodeList.add(deliveryScanInfoBeans.get(j).getLableNumber());
                    }
                    materialList.setTagCodeList(tagCodeList);
                    materialListList.add(materialList);
                }
                deliverySubmitParams.setMaterialList(materialListList);
                System.out.println("出库入参json-----------------"+deliverySubmitParams.toString());
                //调用接口
                mViewModel.deliverySubmit(deliverySubmitParams).observe(this,resourse->resourse.handler(new OnCallback<ResponModel>() {
                    @Override
                    public void onSuccess(ResponModel data) {
                            ToastUtils.showToast("出库提交成功");
                            //提交成功

                            //清除数据
                            List<DeliveryInfoBean> deliveryInfoBeanListDelete =deliveryInfoDao.getDeliveryByOrderNumber(deliveryOrderId);
                            for(int i=0;i<deliveryInfoBeanListDelete.size();i++){
                                //删除标签信息
                                List<DeliveryScanInfoBean> deliveryScanInfoBeansDelete=  deliveryScanDao.getAll(deliveryInfoBeanListDelete.get(i).getOrderNumber());
                                List<ScanDeliveryBean>  scanDeliveryBeanList=scanDeliveryDao.getScanDeliveryBeanByOddNumber(deliveryInfoBeanListDelete.get(i).getOddNumber());
                                for (int j=0;j<deliveryScanInfoBeansDelete.size();j++){
                                    DeliveryScanInfoBean deliveryScanInfoBean= deliveryScanInfoBeansDelete.get(j);
                                    deliveryScanDao.delete(deliveryScanInfoBean);
                                }
                                for (int a=0;a<scanDeliveryBeanList.size();a++){
                                    ScanDeliveryBean scanDeliveryBean= scanDeliveryBeanList.get(a);
                                    scanDeliveryDao.delete(scanDeliveryBean);
                                }
                                DeliveryInfoBean deliveryInfoBean=deliveryInfoBeanListDelete.get(i);
                                deliveryInfoDao.delete(deliveryInfoBean);
                            }
                            //设置false
                            isDeliveryScan=false;
                            //清除Sp本地缓存数据
                            SharedPreferences.Editor editor=sharedPreferences.edit();
                            editor.clear().commit();
                             finish();
                    }
                }));

            }
        }else {
            ToastUtils.showToast("未满足提交物料的条件,请核对好再提交");
        }

    }
    private boolean isSubmit(){
        List<Boolean> isBool=new ArrayList<>();
        //查询本地数据库数据=是否可以提交
        List<DeliveryInfoBean> deliveryInfoBeanLis= deliveryInfoDao.getDeliveryByOrderNumber(deliveryOrderId);
        System.out.println("666666666666666666666666"+deliveryInfoBeanLis.toString());
        if(deliveryInfoBeanLis.size()>0) {
            for (int i = 0; i < deliveryInfoBeanLis.size(); i++) {
                System.out.println("9999999999999999999999999"+deliveryInfoBeanLis.toString());
                if (deliveryInfoBeanLis.get(i).getSumNumber() == deliveryInfoBeanLis.get(i).getRealToNumber()) {
                    isBool.add(true);
                } else {
                    isBool.add(false);
                }
            }
            for (boolean isBools: isBool){
                if(!isBools){
                    return false;
                }
            }
            return true;
        }else {
            ToastUtils.showToast("没有可提交的物料");
        }

        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}
